(function(w) {
	function plusReady() {
		// console.log("plusReady");
	};
	if (w.plus) {
		plusReady();
	} else {
		document.addEventListener("plusready", plusReady, false);
	};
	var packName = null;
	var dtask = null;
	var partyMethod = {
		
		/**
		 * 检查app是否安装
		 * @param  {String} pg 包名
		 * @return {Number} int 1 已安装，2 未安装
		 */
		isInstall: function(pg) {
			// console.log("checkAppInstalled" + pg);
			this.packName = pg;
			var b = plus.runtime.isApplicationExist({
				pname: pg
			}) ? 1 : 2;
			console.log("checkAppInstalled  是否已安装 " + this.packName + b);
			return b;
		},
		

		/**
		 * 执行关闭退出相关逻辑
		 */
		exit : function() {
			console.log('123');
			plus.activityExit();
			// webView.addJavascriptInterface(new Object(){
			//     @JavascriptInterface
			//     public void exit(){
			//         //退出webview所在页面
			//         activityExit();
			//     }
			// },"partyMethod");
		},
		/**
		 * 打开app
		 * @param  {String} pg 包名
		 * @return {void}
		 */
		openApp: function(pg) {
			console.log('startApp' + pg);
			if (plus.os.name == "Android") {
				plus.runtime.launchApplication({
					pname: pg
				}, function(e) {
					alert("Open system default browser failed: " + e.message);
				});
			}
		},
		/**
		 * 返回按钮监听
		 */
		
		// console.log('123');
		
		/**
		 * ios打开url
		 * @param  {String} url 地址
		 * @return {void}
		 */
		openUrl: function(url) {
			plus.runtime.openURL(url);
		},
		
		/**
		 * 安卓下载
		 * @param  {String} down_url  下载地址
		 * @return {void}
		 */
		download: function(down_url) {
			
			var index = down_url.lastIndexOf('/') > down_url.lastIndexOf('?') ? down_url.lastIndexOf('/') : down_url.lastIndexOf(
				'?');
			var apk_name = "_downloads/" + down_url.substring(index + 1, down_url.length);
			if (apk_name.indexOf(".apk") == -1) {
				apk_name = apk_name + '.apk';
			}
			console.log("下载文件 " + apk_name);
			if (dtask != null && dtask.url === down_url) {
				console.log("任务正在下载中！")
				return;
			}
			
			var options = {
				method: "GET",
				filename: apk_name
			};
			console.log("开始创建下载任务 ");
			var precent = 0;
			dtask = plus.downloader.createDownload(down_url, options, null);
			dtask.addEventListener("statechanged", function(task, status) {
				if (!dtask) {
					return;
				}
				switch (task.state) {
					case 1: // 开始
						console.log("开始下载 ...");
						console.log("开始下载 ...->" + precent);
						precent = 0;
						break;
					case 2: // 已连接到服务器
						console.log("链接到服务器... ");
						console.log("链接到服务器 ...->" + precent);
						precent = 0;
						break;
					case 3: // 已接收到数据
						var cur_precent = parseInt(100 * task.downloadedSize / task.totalSize) //Math.round(task.downloadedSize * 100 / task.totalSize)
						if (precent != cur_precent) {
							precent = cur_precent;
							console.log("正在下载…… packageName->progress->" + precent + "%");
							onProgress(down_url, 1, task.downloadedSize, task.totalSize);
						}
						break;
					case 4: // 下载完成
						var filepath = task.filename;
						console.log("下载完成 " + filepath);
						 onProgress(down_url, 2);
						var install_options = {
							force: false
						};
						plus.runtime.install(filepath, install_options, function(widgetInfo) {
							console.log("安装成功");
							dtask = null;
							console.log(dtask == null);
						}, function(e) {
							console.log("安装失败" + e.message);
							dtask = null;
							console.log(dtask == null);
						});
						break;
				}
			});
			dtask.start();
			console.log("下载任务创建成功 ");
		}
	};
	w.partyMethod = partyMethod;
})(window);